package com.mc.algorithm;

public class BinaryRepresent {
	
	public static int compute(int n){
		int count=0;
		while(n!=0){
			if((n&1)==1)
				count++;
			n = n>>1;
		}
		return count;
	}
	public static int computeLeft(int n){
		int count=0;
		int flag=1;
		while(flag!=0){
			if((n&flag)==1)
				count++;
			flag = flag<<1;
		}
		return count;
	}
	public static int compute2(int n){
		int count=0;
		while(n!=0){
			n = n&(n-1);
			count++;
		}
		return count;
	}
	public static double compute3(double base, int exp){
		if(eq(base,0)&&exp<0)
			return -1;
		int absExp = exp;
		if(exp<0)
			absExp = -exp;
		double result = expo(base,absExp);
		if(exp<0)
			result = 1/result;
		return result;
	}
	private static double expo(double base, int exp){
		if(exp==0)
			return 1;
		if(exp==1)
			return base;
		double result = expo(base,exp/2);
		result*=result;
		if((exp&1)==1)//奇数
			result*=base;
		return result;
	}
	private static boolean eq(double a1,double a2){
		if(a1-a2<0.0000001)
			return true;
		return false;
	}
	public static void main(String[] args) {
		System.out.println(BinaryRepresent.compute3(2, 10));
	}
}
